<template>
	<view class='jieguo_view' v-if="show_status==1">
		<view class="jieguo_content">
			<image src="./img/bg_dz_bcss.png" class="jieguo_content_bg"></image>
			<view class="jieguo_content_btn_close" @tap="jieguo_content_close"></view>
			<view class="jieguo_tishi">已自动替换您的个人信息</view>
			<!-- 免费用户需要购买海报才能下载 -->
			<view v-if="user_info.info.type==0">
				<view v-if="sys_ios==0">
					<view class="jieguo_tishi_1">您需要购买此海报才能下载</view>
					<view class="jieguo_tishi_2">一次购买永久免费下载此海报</view>
					<view class="btns_view">
						<view class="btns_view_left" @tap="go_vip">开通VIP</view>
						<view class="btns_view_right" @tap="pay">￥{{muban.jiage}}购买</view>
					</view>
				</view>
				<view v-else-if="sys_ios==1">
					<view class="jieguo_tishi_1">会员专属海报,您暂无权限下载</view>
					<view class="jieguo_tishi_2">如遇问题,请联系客服</view>
					<view class="btns_view">
						<view class="btns_view_left" @tap="jieguo_content_close">取消</view>
						<button class="btns_view_right btn_kefu" open-type='contact'>联系客服</button>
					</view>
				</view>
			</view>
			<!-- 免费用户使用免费次数下载 -->
			<view v-else-if="user_info.info.type==1">
				<view class="jieguo_tishi_1">可使用免费下载次数{{user_info.left}}次</view>
				<view class="jieguo_tishi_2">分享给好友可获得更多下载次数</view>
				<view class="btns_view">
					<view class="btns_view_left" @tap="jieguo_content_close">取消</view>
					<view class="btns_view_right" @tap="download_zuopin">下载</view>
				</view>
			</view>
			<!-- 设计师下载 -->
			<view v-else-if="user_info.info.type==2">
				<view class="jieguo_tishi_1">您是设计师</view>
				<view class="jieguo_tishi_2">{{zuopinid==0?'可以无限制下载海报':'是否将该作品保存为范例'}}</view>
				<view class="btns_view" v-if="zuopinid==0">
					<view class="btns_view_left" @tap="jieguo_content_close">取消</view>
					<view class="btns_view_right" @tap="download_zuopin">下载</view>
				</view>
				<view class="btns_view" v-else>
					<view class="btns_view_left" @tap="download_zuopin">下载</view>
					<view class="btns_view_right" @tap="save_fanli">保存</view>
				</view>
			</view>
			<!-- 会员使用会员下载次数下载 -->
			<view v-else-if="user_info.info.type==3">
				<view class="jieguo_tishi_1">{{user_info.info.msg}}</view>
				<view class="jieguo_tishi_2">今日剩余{{user_info.dleft}}次</view>
				<view class="btns_view">
					<view class="btns_view_left" @tap="jieguo_content_close">取消</view>
					<view class="btns_view_right" @tap="download_zuopin">下载</view>
				</view>
			</view>
			<!-- 会员使用会员次数+免费次数下载 -->
			<view v-else-if="user_info.info.type==4">
				<view v-if="user_info.dleft>0">
					<view class="jieguo_tishi_1">{{user_info.info.msg}}</view>
					<view class="jieguo_tishi_2">今日剩余{{user_info.dleft}}次</view>
					<view class="btns_view">
						<view class="btns_view_left" @tap="jieguo_content_close">取消</view>
						<view class="btns_view_right" @tap="download_zuopin">下载</view>
					</view>
				</view>
				<view v-else>
					<view class="jieguo_tishi_1">VIP每日免费次数已用完</view>
					<view class="jieguo_tishi_2">可使用拉新获得的{{user_info.left}}次</view>
					<view class="btns_view">
						<view class="btns_view_left" @tap="jieguo_content_close">取消</view>
						<view class="btns_view_right" @tap="download_zuopin">下载</view>
					</view>
				</view>
			</view>
			<!-- 会员所有下载次数已用完 -->
			<view v-else-if="user_info.info.type==5">
				<view class="jieguo_tishi_1">可使用下载次数0次</view>
				<view class="jieguo_tishi_2">您可以通过以下方式获得下载权限</view>
				<view class="btns_view" :style="'top:' + (videoAd != null ? 390 : 458) + 'rpx'">
					<button open-type="share" class="btns_view_left">分享好友</button>
					<view class="btns_view_right" @tap="go_vip">{{user_info.vip==0?'开通会员':'升级会员'}}</view>
				</view>
				<view class="btns_show_video" @tap="wana_show_ad" v-if="videoAd!=null">观看广告</view>
			</view>
			<!-- 已购买模板可以无限制下载 -->
			<view v-else-if="user_info.info.type==6">
				<view class="jieguo_tishi_1">您已经购买此海报</view>
				<view class="jieguo_tishi_2">可以无限制免费下载</view>
				<view class="btns_view">
					<view class="btns_view_left" @tap="jieguo_content_close">取消</view>
					<view class="btns_view_right" @tap="download_zuopin">下载</view>
				</view>
			</view>
			<!-- 免费模板，用户会员等级不够 -->
			<view v-else-if="user_info.info.type==7">
				<view v-if="sys_ios==0">
					<view class="jieguo_tishi_1">{{user_info.vip==0?'Vip专享海报':'您的会员等级不够'}}</view>
					<view class="jieguo_tishi_2">需要{{vip_can_str[user_info.info.vipcan]}}才能下载</view>
					<view class="btns_view">
						<view class="btns_view_left" @tap="jieguo_content_close">取消</view>
						<view class="btns_view_right" @tap="go_vip">{{user_info.vip==0?'开通会员':'升级会员'}}</view>
					</view>
				</view>
				<view v-else-if="sys_ios==1">
					<view class="jieguo_tishi_1">{{user_info.vip==0?'Vip专享海报':'您的会员等级不够'}}</view>
					<view class="jieguo_tishi_2">需要{{vip_can_str[user_info.info.vipcan]}}才能下载</view>
					<view class="btns_view">
						<view class="btns_view_left" @tap="jieguo_content_close">取消</view>
						<button class="btns_view_right btn_kefu" open-type='contact'>联系客服</button>
					</view>
				</view>
			</view>
			<!-- 免费用户需要购买海报才能下载 -->
			<view v-else-if="user_info.info.type==8">
				<view v-if="sys_ios==0">
					<view class="jieguo_tishi_1">{{user_info.vip==0?'Vip专享海报':'您的会员等级不够'}}</view>
					<view class="jieguo_tishi_2">需要{{vip_can_str[user_info.info.vipcan]}}才能下载</view>
					<view class="btns_view">
						<view class="btns_view_left" @tap="jieguo_content_close">取消</view>
						<view class="btns_view_right" @tap="go_vip">{{user_info.vip==0?'开通会员':'升级会员'}}</view>
					</view>
				</view>
				<view v-else-if="sys_ios==1">
					<view class="jieguo_tishi_1">{{user_info.vip==0?'Vip专享海报':'您的会员等级不够'}}</view>
					<view class="jieguo_tishi_2">需要{{vip_can_str[user_info.info.vipcan]}}才能下载</view>
					<view class="btns_view">
						<view class="btns_view_left" @tap="jieguo_content_close">取消</view>
						<button class="btns_view_right btn_kefu" open-type='contact'>联系客服</button>
					</view>
				</view>
			</view>
			<!-- 免费用户次数不够 -->
			<view v-else-if="user_info.info.type==9">
				<view v-if="sys_ios==1">
					<view class="jieguo_tishi_1">可使用下载次数0次</view>
					<view class="jieguo_tishi_2">您可以通过以下方式获得下载权限</view>
					<view class="btns_view" :style="'top:' + (videoAd != null ? 390 : 458) + 'rpx'">
					  <button open-type="share" class="btns_view_left">分享好友</button>
					  <button class="btns_view_right btn_kefu" open-type="contact">联系客服</button>
					</view>
					<view class="btns_show_video" @tap="wana_show_ad" v-if="videoAd!=null">观看广告</view>
				</view>
				<view v-else>
					<view class="jieguo_tishi_1">可使用下载次数0次</view>
					<view class="jieguo_tishi_2">您可以通过以下方式获得下载权限</view>
					<view class="btns_view" :style="'top:' + (videoAd != null ? 390 : 458) + 'rpx'">
						<button open-type="share" class="btns_view_left">分享好友</button>
						<view class="btns_view_right" @tap="go_vip">{{user_info.vip==0?'开通会员':'升级会员'}}</view>
					</view>
					<view class="btns_show_video" @tap="wana_show_ad" v-if="videoAd!=null">观看广告</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: '',
		props: {
			videoAd: {
				type: Object,
				value: null
			},
			zuopinid: {
				type: Number,
				value: 0,
			},
			mbid: {
				type: Number,
				value: 0,
			},
			show_status: {
				type: Number,
				value: 0,
			}
		},
		watch: {
			show_status: {
				handler(newval, oldval) {
					if (newval == 1) {
						this.inite();
					} else {
						this.setData({
							show_status: newval,
						})
					}
				},
				deep: true,
				immediate: true
			}
		},
		data() {
			return {
				show_status: 0,
				muban: {},
				user_info: {},
				videoAd: null,
				sys_ios: 1,
				vip_can_str: [],
				has_inite_ad: 0
			}
		},
		onLoad() {

		},
		onShow() {

		},
		methods: {
			inite: function() {
				console.log('初始化结果窗口');
				var that = this;
				wx.getSystemInfo({
					success(res) {
						console.log(res.system)
						if (res.system.indexOf("iOS") != -1) {
							console.log('苹果系统');
						} else {
							that.setData({
								sys_ios: 0
							})
							console.log('安卓系统');
						}
					}
				})
				var data = new Object();
				data.apiname = 'inite';
				data.mbid = this.properties.mbid;
				data.zuopinid = this.properties.zuopinid;
				app.mlib.request({
					'model': 'download',
					'data': data,
					'cachetime': '0',
					success(res) {
						console.log(res);
						console.log(res.data.data.user);
						if (res.data.data.r == 0) {
							wx.showToast({
								title: '获取失败',
								icon: 'none',
							})
							that.setData({
								show_status: 0,
							})
							return;
						}
						that.setData({
							user_info: res.data.data.user,
							muban: res.data.data.muban,
							show_status: 1,
							vip_can_str: res.data.data.vip_name,
						})
					},
					fail(res) {
						console.log(res);
					}
				})
			},
			/**
			 * 将作品保存为范例
			 */
			save_fanli: function() {
				var that = this;
				var data = new Object();
				data.apiname = 'fanli';
				data.zuopinid = this.properties.zuopinid;
				app.mlib.request({
					'model': 'zuopin',
					'data': data,
					'cachetime': '0',
					success(res) {
						console.log(res);
						that.setData({
							show_status: 0
						})
						wx.showToast({
							title: res.data.data.msg,
						})
					},
					fail(res) {
						console.log(res);
					}
				})
			},
			/**
			 * 支付
			 */
			pay: function() {
				var data = new Object();
				data.type = 3;
				data.mbid = this.data.muban.id;
				var that = this;
				app.util.request({
					'url': 'entry/wxapp/pay',
					'data': data,
					'cachetime': '0',
					success(res) {
						console.log(res);
						if (res.data.data.r == 2) //余额足够，不需要支付
						{
							that.data.user_info.info.type = 6;
							that.data.user_info.can = 1;
							that.setData({
								user_info: that.data.user_info,
							})
							that.download_zuopin();
							wx.showToast({
								title: '支付成功',
							})
							return;
						}
						wx.requestPayment({
							'timeStamp': res.data.data.timeStamp,
							'nonceStr': res.data.data.nonceStr,
							'package': res.data.data.package,
							'signType': 'MD5',
							'paySign': res.data.data.paySign,
							'success': function(res) {
								wx.showToast({
									title: '支付成功',
								})
								that.data.user_info.info.type = 6;
								that.data.user_info.can = 1;
								that.setData({
									user_info: that.data.user_info,
								})
								that.download_zuopin();
							},
							'fail': function(res) {
								wx.showToast({
									title: '支付失败',
								})
							}
						})
					},
					fail(res) {
						console.log(res);
					}
				})
			},
			/**
			 * 前往会员中心
			 */
			go_vip: function(e) {
				this.setData({
					show_status: 0,
				})
				wx.navigateTo({
					url: '../vip/vip',
				})
			},
			wana_show_ad: function() {
				this.jieguo_content_close();
				this.triggerEvent('wanna_show_ad');
			},
			/**
			 * 下载作品
			 */
			download_zuopin: function(e) {
				if (this.data.user_info.can == 0) {
					console.log('不能下载');
					return;
				}
				var that = this;
				wx.getSetting({
					success(res) {
						console.log(res.authSetting['scope.writePhotosAlbum']);
						if (res.authSetting['scope.writePhotosAlbum'] == false) {
							that.data.no_need_refresh = 1;
							wx.openSetting({
								success(res) {
									that.data.no_need_refresh = 0;
									console.log(res.authSetting['scope.writePhotosAlbum'])
									if (res.authSetting['scope.writePhotosAlbum'] == true) {
										that._download_zuopin();
									}
								}
							})
						} else if (res.authSetting['scope.writePhotosAlbum'] == undefined) {
							console.log('请求写入权限');
							wx.authorize({
								scope: 'scope.writePhotosAlbum',
								success(res) {
									console.log(res);
									that._download_zuopin();
								},
								fail: function(res) {
									console.log(res);
								},
							})

						} else {
							that._download_zuopin();
						}
					}
				})
			},
			_download_zuopin: function() {
				this.setData({
					show_status: 0,
				})
				if (this.properties.zuopinid != 0) {
					this.down_zuopin();
				} else {
					this.down_muban();
				}
			},
			down_pics: function(scb, fcb) {
				var cur_down_index = this.data.cur_down_index;
				var pics_down = this.data.pics_down;
				console.log(cur_down_index);
				console.log(pics_down);
				if (pics_down.length == 0) {
					fcb();
					return;
				}
				var temp = cur_down_index * 1 + 1;
				var loading_content = '保存中' + temp + "/" + pics_down.length;
				wx.showLoading({
					title: loading_content,
				})
				if (cur_down_index * 1 < pics_down.length) {
					var that = this;
					console.log('开始下载');
					wx.downloadFile({
						url: pics_down[cur_down_index],
						success(res) {
							console.log(res);
							if (res.statusCode === 200) {
								console.log('开始保存');
								wx.saveImageToPhotosAlbum({
									filePath: res.tempFilePath,
									success(res) {
										console.log(res.errMsg);
										that.data.cur_down_index++;
										that.down_pics(scb, fcb);
									},
									fail(res) {
										console.log(res)
									}
								})
							} else {
								console.log('下载失败');
								fcb();
							}
						}
					})

				} else {
					scb();
				}
			},
			down_zuopin: function() {
				var that = this;
				var data = new Object();
				data.apiname = 'downloadzuopin';
				data.zuopinid = this.properties.zuopinid;
				app.mlib.request({
					'model': 'download',
					'data': data,
					'cachetime': '0',
					success(res) {
						console.log(res);
						if (res.data.data.r == 0) {
							wx.showToast({
								title: res.data.data.msg,
							})
							return;
						}
						that.setData({
							user_info: res.data.data.user,
							show_status: 0,
						})
						wx.showLoading({
							title: '下载中',
						})
						wx.getImageInfo({
							src: res.data.data.url,
							success(res) {
								wx.hideLoading();
								that.setData({
									show_status: 0,
								})
								console.log(res)
								console.log(res.path);
								wx.saveImageToPhotosAlbum({
									filePath: res.path,
									success(res) {
										wx.showToast({
											title: '保存成功',
										})
									}
								})
							}
						})
					},
					fail(res) {
						console.log(res);
					}
				})
			},
			down_muban: function() {
				var that = this;
				var data = new Object();
				data.apiname = 'downloadmuban';
				data.id = this.data.muban.id;
				console.log(data);
				app.mlib.request({
					'model': 'download',
					'data': data,
					'cachetime': '0',
					success(res) {
						console.log(res);
						if (res.data.data.r == 0) {
							wx.showToast({
								title: res.data.data.msg,
							})
							return;
						}
						that.setData({
							user_info: res.data.data.user,
						})
						wx.getImageInfo({
							src: res.data.data.url,
							success(res) {
								that.setData({
									show_status: 0,
								})
								console.log(res)
								console.log(res.path);
								wx.saveImageToPhotosAlbum({
									filePath: res.path,
									success(res) {
										wx.showToast({
											title: '保存成功',
										})
									}
								})
							}
						})
					},
					fail(res) {
						console.log(res);
					}
				})
			},
			/**
			 * 关闭结果窗口
			 */
			jieguo_content_close: function() {
				this.setData({
					show_status: 0,
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.jieguo_view {
		width: 750rpx;
		height: 100%;
		position: fixed;
		left: 0rpx;
		top: 0rpx;
		background-color: rgba(0, 0, 0, 0.3);
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		z-index: 999;
	}

	.jieguo_content {
		width: 504rpx;
		height: 578rpx;
		position: relative;
	}

	.jieguo_content_bg {
		width: 504rpx;
		height: 578rpx;
		position: absolute;
		left: 0rpx;
		top: 0rpx;
	}

	.jieguo_content_btn_close {
		width: 80rpx;
		height: 80rpx;
		position: absolute;
		top: 0rpx;
		right: 0rpx;
	}

	.jieguo_tishi {
		font-size: 28rpx;
		font-family: Adobe Heiti Std;
		font-weight: normal;
		color: rgba(51, 51, 51, 1);
		position: absolute;
		left: 49rpx;
		top: 114rpx;
	}

	.jieguo_tishi_1 {
		width: 504rpx;
		position: absolute;
		left: 0rpx;
		top: 285rpx;
		text-align: center;
		height: 34rpx;
		line-height: 34rpx;
		font-size: 36rpx;
		font-family: SimHei;
		font-weight: 400;
		color: rgba(68, 68, 68, 1);
	}

	.jieguo_tishi_2 {
		width: 484rpx;
		position: absolute;
		left: 10rpx;
		top: 344rpx;
		text-align: center;
		height: 31rpx;
		font-size: 32rpx;
		font-family: SimHei;
		font-weight: 400;
		color: rgba(56, 56, 56, 1);
	}

	.btns_view {
		width: 504rpx;
		position: absolute;
		left: 0rpx;
		top: 458rpx;
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
	}

	.btns_view_left {
		width: 186rpx;
		height: 76rpx;
		line-height: 76rpx;
		text-align: center;
		border: 2rpx solid rgba(201, 208, 218, 1);
		border-radius: 10rpx;
		margin: 0 40rpx 0 0;
		font-size: 32rpx;
		font-family: SimHei;
		font-weight: 400;
		color: rgba(57, 57, 57, 1);
		padding: 0rpx;
	}

	.btns_view_right {
		width: 190rpx;
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
		background: rgba(255, 221, 2, 1);
		border-radius: 10rpx;
		font-size: 32rpx;
		font-family: SimHei;
		font-weight: 400;
		color: rgba(57, 57, 57, 1);
	}

	.btns_view .btn_kefu {
		margin: 0rpx;
		width: 190rpx;
		height: 80rpx;
		line-height: 80rpx;
		color: rgba(57, 57, 57, 1);
		font-size: 32rpx;
	}

	.btns_show_video {
		width: 420rpx;
		position: absolute;
		left: 42rpx;
		top: 485rpx;
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
		background: rgba(255, 221, 2, 1);
		border-radius: 10rpx;
		color: rgba(57, 57, 57, 1);
	}
</style>